package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderSettingService;
import com.itheima.service.SetmealService;
import com.itheima.utils.POIUtils;
import com.itheima.utils.QiniuUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            if(readExcel!=null && readExcel.size()>0){
                List<OrderSetting> list = new ArrayList<>();
                for (String[] strings : readExcel) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    list.add(orderSetting);
                }
                orderSettingService.add(list);
                return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }

    @RequestMapping("/getOrdersettingByMonth")
    public Result getOrdersettingByMonth(String date){
        try {
            List<Map> list = orderSettingService.getOrdersettingByMonth(date);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 修改预约数量
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    //这里参数这么写是因为前台传过来的number和date 正好能和这个实体类的参数对应上 所以可以直接写成实体类
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 思路：
     * 1、查询当前t_ordersetting表中所有数据
     * 2、将所有数据写在模板上
     * 3、前台下载
     * 4、下载成功后将模板内容清空
     * 文件下载
     * @return
     */
    @RequestMapping("/download")
    public Result download(String date){

        try {
            /*File file = new File("E:\\health_parent\\health_parent\\health_backend\\src\\main\\webapp\\template\\ordersetting_template_bak.xlsx");
            if (file.exists()) {
                file.delete();
                System.out.println("文件已经被成功删除");
            }*/
            XSSFWorkbook sheets = new XSSFWorkbook(new FileInputStream(new File("E:\\health_parent\\health_parent\\health_backend\\src\\main\\webapp\\template\\ordersetting_template.xlsx")));
            sheets.setSheetName(0,date+"数据");
            XSSFSheet sheet = sheets.getSheetAt(0);
            //获取列数
//            int cells = sheet.getRow(0).getPhysicalNumberOfCells();
            List<OrderSetting> all = orderSettingService.findAll(date);
            //循环行
            for (int i = 0; i < all.size(); i++) {
                XSSFRow row1 = sheet.createRow(i+2);
                OrderSetting orderSetting = all.get(i);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                row1.createCell(1).setCellValue(sdf.format(orderSetting.getOrderDate()));
                row1.createCell(2).setCellValue(orderSetting.getNumber());
                //循环列
//                for (int j = 0; j <cells; j++) {
//                    row1.createCell(j+1).setCellValue(String.valueOf(all.get(j)));
//                }
            }
            FileOutputStream fos = new FileOutputStream("E:\\health_parent\\health_parent\\health_backend\\src\\main\\webapp\\template\\ordersetting_template_bak.xlsx");
            sheets.write(fos);
            fos.flush();
            fos.close();
            sheets.close();
            return new Result(true,"下载成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"下载失败！");
        }
        /*try {
            orderSettingService.download();

        } catch (Exception e) {
            e.printStackTrace();

        }*/
    }

}
